package com.lunesu.bjair;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;

public class BeijingAirSettingsActivity extends Activity {
	
    private static final String PREFS_NAME =
    	"com.lunesu.bjair.BeijingAirAppWidgetProvider";
    private static final String PREF_PREFIX_KEY = "update_";

    private static int UPDATE_DEFAULT = 2;	// 2 hours
    
	int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
	RadioGroup mAppWidgetPrefix;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
        // Set the result to CANCELED.  This will cause the widget host to cancel
        // out of the widget placement if they press the back button.
        setResult(RESULT_CANCELED);
        
        // Set the view layout resource to use.
        this.setContentView(R.layout.bjair_settings);

        // Find the EditText
        mAppWidgetPrefix = (RadioGroup)findViewById(R.id.RadioGroup01);
        
        //findViewById(R.id.RadioGroup01).setOnClickListener(l);
        findViewById(R.id.Button01).setOnClickListener(mOnClickListener);
        
		// Find the widget id from the intent. 
        Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null)
		    mAppWidgetId = extras.getInt(
		            AppWidgetManager.EXTRA_APPWIDGET_ID, 
		            AppWidgetManager.INVALID_APPWIDGET_ID);
		
        // If they gave us an intent without the widget id, just bail.
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
            finish();
        
        int updatetime = loadSetting(BeijingAirSettingsActivity.this, mAppWidgetId);
        mAppWidgetPrefix.check(getRadioButtonId(updatetime));
	}
	
	static int getRadioButtonId(int updatetime)
	{
		switch (updatetime)
		{
		case -1:
			return R.id.RadioButton01;
		case 1:
			return R.id.RadioButton04;
		case 2:
			return R.id.RadioButton03;
		case 4:
			return R.id.RadioButton05;
		case 24:
			return R.id.RadioButton02;
		default:
			return -1;
		}
	}
	
	
    View.OnClickListener mOnClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            final Context context = BeijingAirSettingsActivity.this;

            // When the button is clicked, save the string in our prefs and return that they
            // clicked OK.
            int checked = mAppWidgetPrefix.getCheckedRadioButtonId();
            if (checked >= 0)
            {
            	String tag = (String)BeijingAirSettingsActivity.this.findViewById(checked).getTag().toString();
            	try {
            		int updatetime = Integer.parseInt(tag);
            		saveSetting(context, mAppWidgetId, updatetime);
            	}
            	catch (NumberFormatException e) {
            		// ignore
            	}
            }
            
            // Push widget update to surface with newly set prefix
            //AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            //BeijingAirAppWidgetProvider.updateWidget(context, mAppWidgetId, titlePrefix);

            // Make sure we pass back the original appWidgetId
            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
            setResult(RESULT_OK, resultValue);
            finish();
        }
    };

	
    // Write the prefix to the SharedPreferences object for this widget
    static void saveSetting(Context context, int appWidgetId, int updatetime) {
        SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
        prefs.putInt(PREF_PREFIX_KEY + appWidgetId, updatetime);
        prefs.commit();
    }

    // Read the prefix from the SharedPreferences object for this widget.
    // If there is no preference saved, get the default from a resource
    static int loadSetting(Context context, int appWidgetId) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
        return prefs.getInt(PREF_PREFIX_KEY + appWidgetId, UPDATE_DEFAULT);
    }
}
